<template>
  <div class="pics-container">
    <!-- 缩略图列表 -->
    <div class="thumbnail-list">
      <el-image
        v-for="(img, index) in pics"
        :key="index"
        :src="img"
        :alt="`图片 ${index + 1}`"
        class="thumbnail-item"
        @click="handleClickImage(index)"
        preview="false"
      >
        <template #loading>
          <div class="image-loading">
            <!-- <el-loading size="small" /> -->
          </div>
        </template>
        <template #error>
          <div class="image-error">
            <el-icon size="20"><PictureFilled /></el-icon>
          </div>
        </template>
      </el-image>
    </div>

    <!-- 放大蒙层 -->
    <div v-if="showOverlay" class="image-overlay" @click="handleCloseOverlay">
      <div class="image-overlay-content" @click.stop>
        <el-image
          :src="currentImage"
          :alt="`放大查看图片 ${currentIndex + 1}`"
          fit="contain"
          class="enlarged-image"
        />
        <!-- 关闭按钮 -->
        <button class="close-btn" @click="handleCloseOverlay">
          <el-icon size="24"><Close /></el-icon>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElImage, ElIcon } from "element-plus";
import { PictureFilled, Close } from "@element-plus/icons-vue";

// 接收图片列表参数
const props = defineProps<{
  pics: string[];
}>();

// 状态管理
const showOverlay = ref(false); // 蒙层显示状态
const currentIndex = ref(0); // 当前显示的图片索引
const currentImage = ref(""); // 当前显示的图片地址

// 点击缩略图显示蒙层
const handleClickImage = (index: number) => {
  currentIndex.value = index;
  currentImage.value = props.pics[index];
  showOverlay.value = true;
  // 禁止页面滚动
  document.body.style.overflow = "hidden";
};

// 关闭蒙层
const handleCloseOverlay = () => {
  showOverlay.value = false;
  // 恢复页面滚动
  document.body.style.overflow = "";
};
</script>

<style scoped>
.pics-container {
  margin: 16px 0;
}

.thumbnail-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.thumbnail-item {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 4px;
  cursor: zoom-in;
  transition: all 0.2s ease;
  border: 1px solid #eee;
}

.thumbnail-item:hover {
  transform: scale(1.03);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 蒙层样式 */
.image-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  opacity: 0;
  animation: fadeIn 0.3s ease forwards;
}

/* 淡入动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.image-overlay-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
}

.enlarged-image {
  max-width: 100%;
  max-height: 80vh;
  border-radius: 4px;
}

/* 关闭按钮 */
.close-btn {
  position: absolute;
  top: -40px;
  right: -40px;
  background: transparent;
  border: none;
  color: #fff;
  cursor: pointer;
  transition: transform 0.2s ease;
  padding: 4px;
}

.close-btn:hover {
  transform: scale(1.1);
}

/* 加载状态样式 */
.image-loading {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.image-error {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  color: #f56c6c;
}
</style>
